package com.cms.admin.action.common;


import com.cms.admin.action.BaseAction;
import com.cms.admin.model.GoodsList;
import com.cms.admin.model.GoodsListExt;

import com.cms.admin.model.PageBean;
import com.cms.admin.service.common.GoodsListService;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;


public class GoodsListAction extends BaseAction implements ModelDriven<GoodsListExt>{

    private GoodsListService goodsListService;
    private TemplateService templateService;
    private GoodsListExt search=new GoodsListExt();
    private PageBean page;

    public String getGoodsid() {
        return goodsid;
    }

    public void setGoodsid(String goodsid) {
        this.goodsid = goodsid;
    }

    private String recid;
    private String goodsid;
    private String goodsIdno;
    private String goodsname;
    private BigDecimal chargeQuantity;


    //修改行
    public  String ajaxEditGoods()
    {
        LOG.debug("Action is:" + search.getAction());
        goodsListService.updateGoods(search);
        return "success";
    }

    //删除行
    public  String ajaxDeleteGoods()
    {
        LOG.debug("Action is:" + search.getAction());
        goodsListService.deleteGoods(search.getRecid());
        return "success";
    }


    //添加行
    public String ajaxAddGoods()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        goodsListService.addGoods(search);
        return "success";
    }

    public String ajaxQueryGoodsList()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        page = new PageBean(search.getCurrentPage(), search.getPageSize());

        List<GoodsListExt> entityList=goodsListService.queryGoodsList(page,search);

        String res=null;

        try {
            res=getQueryResult(entityList,page);
            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        }catch (Exception e) {
            LOG.error("ajaxQueryGoodsList",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }


    private String getQueryResult(List<GoodsListExt> goodsListExts, PageBean page)
            throws Exception {
        HashMap map = new HashMap();
        map.put("goodsList", goodsListExts);
        map.put("page", page);

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_GOODSLIST_QUERY_RESULT, map);
        return result;
    }


    @Override
    public GoodsListExt getModel() {
        return search;
    }


    public GoodsListService getGoodsListService() {
        return goodsListService;
    }

    public void setGoodsListService(GoodsListService goodsListService) {
        this.goodsListService = goodsListService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }
    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public GoodsListExt getSearch() {
        return search;
    }
    public void setSearch(GoodsListExt search) {this.search = search;}

    public PageBean getPage() {
        return page;
    }
    public void setPage(PageBean page) {
        this.page = page;
    }

    public String getGoodsIdno() {
        return goodsIdno;
    }

    public void setGoodsIdno(String goodsIdno) {
        this.goodsIdno = goodsIdno;
    }

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public BigDecimal getChargeQuantity() {
        return chargeQuantity;
    }

    public void setChargeQuantity(BigDecimal chargeQuantity) {
        this.chargeQuantity = chargeQuantity;
    }

    public String getRecid() {
        return recid;
    }

    public void setRecid(String recid) {
        this.recid = recid;
    }
}
